const app = getApp();
var util = require('../../utils/util.js');
var picRoot = app.data.picRoot;
var url = app.data.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    envPic: [],
    roomPic: [],
    beginDayStyle: [{
        month: 'current',
        day: new Date().getDate(),
        color: 'white',
        background: '#AAD4F5'
      },
      {
        month: 'current',
        day: new Date().getDate(),
        color: 'white',
        background: '#AAD4F5'
      }
    ],
    outDayStyle: [{
        month: 'current',
        day: new Date().getDate(),
        color: 'white',
        background: '#84e7d0'
      },
      {
        month: 'current',
        day: new Date().getDate(),
        color: 'white',
        background: '#84e7d0'
      }
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    activeNames: ['1'],
    addr: '广东省云浮市新兴县祥顺大酒店',
    businessTime: '营业时间:每天8点 ~ 每天17点',
    serviceContent: 'aaasssss',
    lat: '',
    lot: '',
    merchantId: '',
    phone: '0766-2899668',
    arr: [],
    name: '',
    detailShow: false,
    roomDetailShow: false,
    inRoomDateShow: false,
    outRoomDateShow: false,
    beginDateStr: util.formatDate(new Date(), 1),
    endDateStr: util.formatDate(new Date(), 0),
    differDay: util.getDifferDay(util.formatDate(new Date(), 0), util.formatDate(new Date(), 1)),
    list1: [],
    list2: [],
    list3: [],
    list4: [],
    list5: [],
    list6: [],
    list7: [],
    list8: [],
    list9: [],
    list10: [],
    list11: [],
    list12: [],
    roomName: '',
    roomDesc: '',
    itemPrice: '',
    itemDiscountPrice: '',
    roomNum: '',
    payType: '',
    totalPrice: ''
  },
  touchMove: function (event) {
    return false;
  },
  emptyBeginTime: function(options) {
    let _that = this;
    _that.setData({
      beginDateStr: ''
    })
  },
  emptyEndTime: function(options) {
    let _that = this;
    _that.setData({
      endDateStr: ''
    })
  },
  confirmBeginDate: function() {
    let _that = this;
    let beginStr = _that.data.beginDateStr;
    if (!beginStr) {
      wx.showToast({
        title: "请选择入住时间",
        icon: "none",
        duration: 2000
      })
      return;
    }
    let endStr = _that.data.endDateStr;
    if (endStr) {
      let differDay = util.getDifferDay(endStr, beginStr);
      if (differDay >= 0) {
        _that.setData({
          differDay: differDay
        })
      }
    }
    _that.setData({
      inRoomDateShow: false
    });
  },
  confirmEndDate: function() {
    let _that = this;
    let endStr = _that.data.endDateStr;
    if (!endStr) {
      wx.showToast({
        title: "请选择离开时间",
        icon: "none",
        duration: 2000
      })
      return;
    }
    let beginStr = _that.data.beginDateStr;
    if (beginStr) {
      let differDay = util.getDifferDay(endStr, beginStr);
      if (differDay >= 0) {
        _that.setData({
          differDay: differDay
        })
      }
    }
    _that.setData({
      outRoomDateShow: false,
    });
  },
  inDayClick: function(event) {
    let _that = this;
    let clickDay = event.detail.day;
    let changeDay = `beginDayStyle[1].day`;
    let changeBg = `beginDayStyle[1].background`;
    let year = event.detail.year;
    let month = event.detail.month;
    month = month < 10 ? ('0' + month) : month;
    let day = event.detail.day;
    day = day < 10 ? ('0' + day) : day;
    let str = year + "-" + month + "-" + day;
    this.setData({
      [changeDay]: clickDay,
      [changeBg]: "#FFdd55",
      beginDateStr: str
    })
    let beginStr = str;
    let endStr = _that.data.endDateStr;
    if (endStr) {
      let differDay = util.getDifferDay(endStr, beginStr);
      if (differDay >= 0) {
        _that.setData({
          differDay: differDay
        })
      }
    }
  },
  outDayClick: function(event) {
    let _that = this;
    let clickDay = event.detail.day;
    let changeDay = `outDayStyle[1].day`;
    let changeBg = `outDayStyle[1].background`;
    let year = event.detail.year;
    let month = event.detail.month;
    month = month < 10 ? ('0' + month) : month;
    let day = event.detail.day;
    day = day < 10 ? ('0' + day) : day;
    let str = year + "-" + month + "-" + day;
    this.setData({
      [changeDay]: clickDay,
      [changeBg]: "#FFdd55",
      endDateStr: str
    })
    let endStr = str;
    let beginStr = _that.data.beginDateStr;
    if (beginStr) {
      let differDay = util.getDifferDay(endStr, beginStr);
      if (differDay >= 0) {
        _that.setData({
          differDay: differDay
        })
      }
    }
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _that = this;
    let merchantId = options.merchantId;
    app.globalData.uuid;
    wx.getStorage({
      key: "uuid",
      success(res) {
        _that.setData({
          merchantId: merchantId,
          uuid: res.data
        })
        let uuid = res.data;
        wx.request({
          url: url + '/HotelSystemApi/queryShowHotelByMerchantId',
          data: {
            merchantId: merchantId,
            uuid: uuid
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            if (res.data.code == 200) {
              console.log(res);
              let item = res.data.data;
              wx.setNavigationBarTitle({
                title: item.merchantName
              })
              let tempEvnList = [];
              let env = item.env;
              if (env != null && env != '') {
                let envArr = item.env.split(",");
                for (var i = 0; i < envArr.length; i++) {
                  tempEvnList.push(picRoot + envArr[i]);
                }
              }
              console.log(item.phone);
              _that.setData({
                lat: item.lat,
                lot: item.lot,
                phone: item.phone,
                addr: item.address,
                envPic: tempEvnList,
                list1: item.list1,
                list2: item.list2,
                list3: item.list3,
                list4: item.list4,
                list5: item.list5,
                list6: item.list6,
                list7: item.list7,
              })

            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 2000
              })
            }

          },
          fail: function(res) {
            console.info('submit fail');
          },
          complete: function(res) {
            console.info('submit complete');
          }
        })
        _that.queryRoomData(uuid, merchantId);
      }
    })


  },

  /**
   * 加载房间
   */
  queryRoomData: function(uuid, merchantId) {
    let _that = this;
    console.log(uuid);
    console.log(merchantId);
    wx.request({
      url: url + '/HotelSystemApi/listPageRoom',
      data: {
        uuid: uuid,
        merchantId: merchantId,
        invoking: 0
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          console.log(res.data.data);

          _that.setData({
            unitList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },

  /**
   * 打开入住时间日历
   */
  openBeginDate: function(options) {
    let _that = this;
    _that.setData({
      inRoomDateShow: true
    });
  },
  closeBeginDate: function() {
    let _that = this;
    _that.setData({
      inRoomDateShow: false
    });
  },

  /**
   * 打开离开时间日历
   */
  openOutDate: function(options) {
    let _that = this;
    _that.setData({
      outRoomDateShow: true
    });
  },
  closeOutDate: function() {
    let _that = this;
    _that.setData({
      outRoomDateShow: false
    });
  },

  /**
   * 打开房间详情
   */
  openRoomDetail: function(options) {
    let _that = this;
    console.log(options);
    let img = options.currentTarget.dataset.img;
    let roomDesc = options.currentTarget.dataset.desc;
    let itemDiscountPrice = options.currentTarget.dataset.itemdiscountprice;
    let itemPrice = options.currentTarget.dataset.itemprice;
    let roomName = options.currentTarget.dataset.name;
    let payType = options.currentTarget.dataset.radio;
    let roomNum = options.currentTarget.dataset.num;
    let id = options.currentTarget.dataset.id;
    let merchantId = _that.data.merchantId;
    let uuid = app.globalData.uuid;
    let imgArr = img.split(",");
    let tempList = [];
    for (var i = 0; i < imgArr.length; i++) {
      if (imgArr[i]) {
        tempList.push(picRoot + imgArr[i]);
      }
    }
    let totalPrice = payType == 0 ? itemPrice : itemDiscountPrice;

    // 查询键值对
    wx.request({
      url: url + '/HotelSystemApi/queryRoomDetail',
      data: {
        uuid: uuid,
        merchantId: merchantId,
        detailId: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          console.log(res.data.data);
          let item = res.data.data
          _that.setData({
            list8: item.list8,
            list9: item.list9,
            list10: item.list10,
            list11: item.list11,
            list12: item.list12,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

    _that.setData({
      totalPrice: totalPrice * 100,
      roomPic: tempList,
      roomName: roomName,
      roomDesc: roomDesc,
      itemDiscountPrice: itemDiscountPrice,
      itemPrice: itemPrice,
      roomNum: roomNum,
      payType: payType,
      roomDetailShow: true
    });
  },
  closeRoomDetail: function() {
    let _that = this;
    _that.setData({
      roomDetailShow: false
    });
  },
  /**
   * 预定
   */
  preBug: function(options) {
    console.log("预定");
  },
  /***
   * 打开酒店详情
   */
  toHotelDetail: function(oiptions) {
    let _that = this;
    _that.setData({
      detailShow: true
    });
  },
  onClose() {
    let _that = this;
    _that.setData({
      detailShow: false
    });
  },
  /***
   * 调起导航
   */
  toLocation: function(options) {
    let _that = this;
    let lat = _that.data.lat;
    let lot = _that.data.lot;
    let addr = _that.data.addr;
    let name = _that.data.name;
    wx.openLocation({
      longitude: Number(lot),
      latitude: Number(lat),
      name: name,
      address: addr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})